package org.netEase.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.netEase.dao.userDao;
import org.netEase.entity.user;
import org.netEase.entity.zhanghu;
import org.netEase.util.BaseDao;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

public class userDaoImpl extends BaseDao implements userDao {

	@Override
	public user checkUser(String name, int password) {
		String sql = "select * from zhanghu where name = ? and password = ? and hid != 1";

		ResultSet rs = super.executeQuery(sql, name,password);
		
		user user = null;
		try {
			if (rs.next()) {
				user = new user();
				user.setHid(rs.getInt("hid"));
				user.setLei(rs.getInt("lei"));
				user.setDataday(rs.getDate("dataday"));
				user.setPassword(rs.getInt("password"));
				user.setNamesp(rs.getString("namesp"));
				user.setName(rs.getString("name"));
				user.setPione(rs.getString("pione"));
				user.setSchoolname(rs.getString("schoolname"));
				user.setSex(rs.getString("sex"));
				user.setStateid(rs.getInt("stateid"));
				user.setToimg(rs.getString("toimg"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return user;
	}

	@Override
	public user getbyhid(int hid) {
		String sql = "select * from zhanghu where hid = ?";
		user user = null;
		ResultSet rs = super.executeQuery(sql, hid);
		try {
			if(rs.next()) {
				user = new user();
				user.setDataday(rs.getDate("dataday"));
				user.setHid(rs.getInt("hid"));
				user.setLei(rs.getInt("lei"));
				user.setNamesp(rs.getString("namesp"));
				user.setPassword(rs.getInt("password"));
				user.setPione(rs.getString("pione"));
				user.setSchoolname(rs.getString("schoolname"));
				user.setSex(rs.getString("sex"));
				user.setStateid(rs.getInt("stateid"));
				user.setToimg(rs.getString("toimg"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				closeAll(rs.getStatement().getConnection(), rs.getStatement(), rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return user;
	}

	@Override
	public int updbyhid(zhanghu zhanghu) {
		String sql = "update zhanghu set lei = ?,namesp = ?,name=?,password=?,toimg=?,sex=?,pione=?,dataday=? where hid = ?";
		return super.executeUpdate(sql, zhanghu.getLei(),zhanghu.getNamesp(),zhanghu.getName(),zhanghu.getPassword(),zhanghu.getToimg(),zhanghu.getSex(),zhanghu.getPione(),zhanghu.getDataday(),zhanghu.getHid());
	}
	@Override
	public int updbyhidtwo(zhanghu zhanghu) {
		String sql = "update zhanghu set toimg=? where hid = ?";
		return super.executeUpdate(sql,zhanghu.getToimg(),zhanghu.getHid());
	}
	
/*	public static void main(String[] args) {
		userDao uDao = new userDaoImpl();
		user user = uDao.checkUser("admin", 123456);
		System.out.println(user.getHid());
	}*/

}
